//---------------------------------------------------------------------
// File: ApplicationStatusStep.cs
// 
// Summary: 
using System;
using System.Xml;
using BizUnit.BizUnitOM;
using Microsoft.BizTalk.ExplorerOM;

namespace BizUnit.Extensions.BizTalk
{
    using Helpers;

    /// <remarks>
    /// The following shows an example of the Xml representation of this test step.
    /// 
    /// <code escaped="true">
    /// <TestStep assemblyPath="BizUnit.Extensions.dll" typeName="BizUnit.Extensions.ApplicationVerifyStatusStep">
    ///		<ApplicationName></ApplicationName>
    ///		<ConnectionString></ConnectionString>
    ///		<Status>NotApplicable|PartiallyStarted|Started|Stopped</Action>
    /// </TestStep>
    /// </code>
    ///	
    ///	<list type="table">
    ///		<listheader>
    ///			<term>Tag</term>
    ///			<description>Description</description>
    ///		</listheader>
    ///		<item>
    ///			<term>DelayForCompletion</term>
    ///			<description>The number of seconds to deplay for this step to complete</description>
    ///		</item>
    ///		<item>
    ///			<term>ApplicationName</term>
    ///			<description>The name of the Application to containing the Application to start/stop</description>
    ///		</item>
    /// 	<item>
    ///			<term>ConnectionString</term>
    ///			<description>The connectionstring to the management database</description>
    ///		</item>
    ///		<item>
    ///			<term>StatusToVerify</term>
    ///			<description>NotApplicable|PartiallyStarted|Started|Stopped</description>
    ///		</item>
    ///	</list>
    ///	</remarks>

    public class ApplicationVerifyStatusStep : ITestStep, ITestStepOM
    {
        private BtsCatalogExplorer root;
        private IBizTalkApplication appInstance;
        private string applicationName;
        private string connectionString;

        private Status statusToVerify;

        public string ApplicationName
        {
            get { return applicationName; }
            set { applicationName = value; }
        }
        public string ConnectionString
        {
            get { return connectionString; }
            set { connectionString = value; }
        }
        public Status StatusToVerify
        {
            get { return statusToVerify; }
            set { statusToVerify = value; }
        }
        /// <summary>
        /// ITestStep.Execute() implementation
        /// </summary>
        /// <param name='testConfig'>The Xml fragment containing the configuration for this test step</param>
        /// <param name='context'>The context for the test, this holds state that is passed beteen tests</param>
        public void Execute(XmlNode testConfig, Context context)
        {
            ApplicationName = context.ReadConfigAsString(testConfig, "ApplicationName");
            ConnectionString = context.ReadConfigAsString(testConfig, "ConnectionString");
            StatusToVerify = (Status)Enum.Parse(typeof(Status), context.ReadConfigAsString(testConfig, "StatusToVerify"), true);
            Execute(context);

        }


        #region ITestStepOM Members

        public void Execute(Context context)
        {
            Validate(context);
            appInstance = BizTalkStepHelper.GetApplication(ApplicationName, ConnectionString);

            if (StatusToVerify != appInstance.Status)
            {
                throw new ApplicationException(string.Format("The application: {0} was expected to be in Status {1}  but was in status {2} ", ApplicationName, statusToVerify, appInstance.Status.ToString()));
            }
            else
            {
                context.LogInfo(string.Format("The application: {0} was found to be in the required status {1} ", ApplicationName, appInstance.Status.ToString()));
            }

        }

        public void Validate(Context context)
        {
            ArgumentValidation.CheckForEmptyString(ApplicationName, "ApplicationName");
            ArgumentValidation.CheckForEmptyString(ConnectionString, "Connection String");
            ArgumentValidation.CheckForEmptyString(StatusToVerify.ToString(), "Status");

        }

        #endregion
    }
}
